<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The MicroNews class for table 'micronews'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2) Inheriting from BaseNews instead of Original DataObject.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_basenews.php';

require_once INSTALLDIR . '/classes/News.php';
require_once INSTALLDIR . '/classes/Picture.php';

class MicroNews extends BaseNews
{
	public $__table = 'micronews';			// table name
	public $id;								// int(11)  primary_key not_null
	public $news_id;						// int(11)  foreign_key unique not_null
	public $user_id;						// int(11)  foreign_key not_null
	public $message;						// text
	public $picture_id;						// int(11)
	public $created;						// datetime()  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('MicroNews', $key, $value); 
	}
	
	/**
	 * Delete the micro news and its realted base news.
	 * Delete picture.
	 */
	function delete()
	{
		$picture = $this->getPicture();
		if ( !empty($picture)) {
			$picture->delete();
		}
		
		parent::delete();
	}
	
	/**
	 * Get the related picture object if exsits.
	 */
	function getPicture()
	{
		return Picture::staticGet('news_id', $this->news_id);
	}
	
	/**
	 * Validate the micro news content.
	 */
	static function validateMessage($message)
	{
		if (empty($message) ||
			mb_strlen($message, 'UTF8') > slh_config('micronews', 'message_maxchar')) {
			return false;
		}
		
		return true;
	}

	/**
	 * Replace the url in the message with real url(<a></a>)!!!
	 */
	static function replaceUrlInMessasge($message)
	{
		// The Regular Expression filter
		$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
		// static $reg_exUrl = "([/w-]+/.)+[/w-]+.([^a-z])(/[/w- ./?%&=]*)?|[a-zA-Z0-9/-/.][/w-]+.([^a-z])(/[/w- ./?%&=]*)?";
 
		// Check if there is a url in the text. Regular expression is from Validate.php.
		if(preg_match($reg_exUrl, $message, $url)) {
			// Add the urls with hyper links.
			return preg_replace($reg_exUrl, "<a href=\"{$url[0]}\" target=\"_blank\">{$url[0]}</a>", $message);
		} else {
	       return $message;
		}
	}
	
	/**
	 * Add the micro news into database.
	 * 
	 * $fields includes: $picture_id, $user_id, $message(htmlstyle?);
	 * Check the $picture_id if the object already belong to some news_id.
	 */
	static function add($fields)
	{
		// Magically put fields into current scope.
		extract($fields);
		
		$news = new News();
		$news->newstype = NEWS_NEWSTYPE_MICRONEWS;
// TODO: The other variables.
		$news->newstag_id = 0;
		$news->status = 0;
		
		$news_id = $news->insert();
		if (empty($news_id)) {
			slh_log_db_error($news, 'INSERT', __FILE__);
			return false;
		}
		$news->id = $news_id;
		
		$micronews = new MicroNews();
		$micronews->news_id = $news_id;
		$micronews->user_id = $user_id;
		
		// Check the picture_id if it's abusive. (If its 'news_id' is set.)
		if ( !Picture::checkIfSetNewsid($picture_id)) {
			$micronews->picture_id = $picture_id;
		}
		
		$micronews->message = $message;		
		$micronews->created = slh_sql_now();
		
		$micronews_id = $micronews->insert();
		if (empty($micronews_id)) {
			slh_log_db_error($micronews, 'INSERT', __FILE__);
			
			@$news->delete();
			return false;
		}
		
		// Set the news_id of the given picture item.
		if ( !empty($micronews->picture_id)) {
			Picture::setNewsidById($micronews->news_id, $micronews->picture_id);
		}
		
		return $micronews_id;
	}
	
	/**
	 * Get the ids as a list. Use offset and limit to control
	 * the pagination.
	 * 
	 * Returning the 'news_id', not the original 'id'.
	 */
	static function getNewsidsByList($offset = 0,
									 $limit = 0)
	{
		return parent::getNewsidsByList(new self(), $offset, $limit);
	}
	
	/**
	 * Get the micro news objects by the given newsids.
	 */
	static function getMicronewsByNewsids($newsids)
	{
		return parent::getSubnewsByNewsids('MicroNews', $newsids);
	}
}
?>